#include <sys/types.h>          
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string>
#include <iostream>
#include <cstring>
#include "my_log.hpp"

#define MAX_COMMAND_LEN 100

int main(int argc, const char** argv)
{
    if (argc != 3)
    {
        std::cout << "Format mismatched, start failed." << std::endl;
        std::cout << "example input: program_name id port" << std::endl;
        lg.basic_print(level::FATAL, std::make_tuple("input format err"), true);
        std::terminate();
    }
    int udp_fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (udp_fd == -1)
    {
        lg.basic_print(level::FATAL, std::make_tuple("connot create socket fd "), true, true);
    }
    struct sockaddr_in _server_info;
    memset(&_server_info, 0, sizeof(struct sockaddr_in));
    _server_info.sin_family = AF_INET;
    _server_info.sin_addr.s_addr = inet_addr(argv[1]);
    _server_info.sin_port = htons(atoi(argv[2]));
    char command[MAX_COMMAND_LEN] = {};
    while (1)
    {
        std::cout << "please enter# ";
        std::cin >> command;
        ssize_t n = sendto(udp_fd, command, strlen(command), 0, static_cast<struct sockaddr*>(static_cast<void*>(&_server_info)), sizeof(struct sockaddr_in));
        n = recv(udp_fd, command, MAX_COMMAND_LEN, 0);
        std::cout << command << std::endl;
    }
    return 0;
}